package com.atguigu.principle.singleresponsibility.singleton.type1;

/**
 * @Author: yangtao
 * @Description:
 */
public class Singleton2 {

    public static void main(String[] args) {
        Singleton instance = Singleton.getInstance();
        Singleton instance2 = Singleton.getInstance();
        //输出返回true
        System.out.println(instance == instance2);
        System.out.println("instance.hashCode=" + instance.hashCode());
        System.out.println("instance2.hashCode=" + instance2.hashCode());
    }
}

//静态变量
class Singleton{
    //构造器私有化
    private Singleton(){


    }
    //创建对象实例
    private  static Singleton  instance;

    static {
        //在静态代码块创建单例对象
        instance = new Singleton();
    }

    //提供公有的静态方法
     public  static Singleton getInstance(){
         return instance;
     }
}
